package com.lainiao.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *  服务端 获取 客户端 返回的Cookie
 *      客户端返回的是 Cookie数组
 */
@WebServlet("/gc")
public class cookie02_GetCookie extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取Cookie数组
        Cookie[] cookies = request.getCookies();
        // 判断Cookies是否为null
        if(cookies!=null && cookies.length>0){
            // 遍历Cookies数组
            for (Cookie cookie:cookies) {
                // 获取cookie的名称和值
                String name = cookie.getName();
                String value = cookie.getValue();
                System.out.println("名称：" + name + "，值：" + value);
            }
        }
    }
}
